package cn.hwadee.muying.service;

import cn.hwadee.muying.entity.FollowLog;
import cn.hwadee.muying.entity.Organization;
import cn.hwadee.muying.entity.Reception;
import cn.hwadee.muying.mapper.FollowLogMapper;
import cn.hwadee.muying.commons.base.BaseService;
import org.springframework.stereotype.Service;
import cn.hwadee.muying.entity.User;
import cn.hwadee.muying.commons.page.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
 * <p>
 * 客户跟进记录 服务实现类
 * </p>
 *
 * @author Mankind
 * @since 2018-08-06
 */
@Service
public class FollowLogService extends BaseService<FollowLogMapper, FollowLog> {

    @Autowired
    private FollowLogMapper followLogMapper;
    @Autowired
    private OrganizationService orgService;
    @Autowired
    private UserService userService;
    @Autowired
    private ReceptionService receptionService;


    public MyPage<FollowLog> list(MyPage<FollowLog> page, User cUser,String keyword) throws Exception{
        List<FollowLog> followLogs = followLogMapper.list(page,cUser,keyword);

        //将id映射成对应的name，显示在前台
        for(FollowLog followLog:followLogs){


            Long orgId=followLog.getOrgId();
            Organization org=orgService.selectById(orgId);
            if(org!=null){
                followLog.setOrgName(org.getName());
            }

            Reception reception=receptionService.selectById(followLog.getReceptionId());
            if(reception!=null){
                followLog.setClientName(reception.getName());
            }

            User user=userService.selectById(followLog.getFollowerId());
            if(user!=null){
                followLog.setFollowerName(user.getUsername());
            }

        }
        page.setRecords(followLogs);
        return page;
    }

}
